home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
45975
/
45975.xpi
/
content
/
weaveIntegration.js
< prev
Wrap
Text File
|
2009-11-23
|
8KB
|
228 lines
/*
* Copyright (c) 2009 Bui Viet Thanh (thanhbv@gmail.com).
*
* This file is part of clicknlearn.
*
* clicknlearn is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* clicknlearn is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with clicknlearn. If not, see <http://www.gnu.org/licenses/>.
*/
//@see https://developer.mozilla.org/en/JavaScript_code_modules/Using_JavaScript_code_modules
const EXPORTED_SYMBOLS = ['CNLEngine', 'CNLRec'];
//const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/util.js");
Cu.import("resource://weave/engines.js");
Cu.import("resource://weave/stores.js");
Cu.import("resource://weave/trackers.js");
Cu.import("resource://weave/base_records/collection.js");
//
Cu.import("resource://weave/base_records/wbo.js");
Cu.import("resource://weave/base_records/crypto.js");
Cu.import("resource://weave/base_records/keys.js");
//
Cu.import("resource://clicknlearn/cnldao.js");
//
Cu.import("resource://clicknlearn/ext/Observers.js");
///////////////////////////////////////////////////////
function CNLRec() {
this._CNLRec_init();
}
CNLRec.prototype = {
__proto__: CryptoWrapper.prototype,
_logname: "Record.CNL",
_CNLRec_init: function CNLItem_init(uri) {
this._CryptoWrap_init(uri);
this.cleartext = {};
},
getDecodeData: function(){
return {"id": decodeURIComponent(this.id), "originalUrl": this.originalUrl
,"remind": decodeURIComponent(this.remind), "read": this.read}
}
};
//TODO add modifyTime to vocabulary DB
Utils.deferGetSet(CNLRec, "cleartext", ["word", "originalUrl", "remind", "read"]);
///////////////////////////////////////////////////////
function CNLEngine() {
this._init();
}
CNLEngine.prototype = {
__proto__: SyncEngine.prototype,
name: "cnl",
_displayName: "CNL",
description: "Clicknlearn vocabulary database sync with Weave.",
logName: "CNL",
_recordObj: CNLRec,
_storeObj: CNLStore,
_trackerObj: CNLTracker,
// _sync: Utils.batchSync("CNL", SyncEngine)
_findDupe: function (item) {
return false;
}
};
///////////////////////////////////////////////////////
// Maintains the store of all your CNL-type items and their GUIDs.
function CNLStore() {
this._init();
}
/**
* Note: because issue "synch fail with vietnamese words" (Issue 4)
* We encodeURIComponent when set word | remind into CNLRec
* & decodeURIComponent when get word | remind from CNLRec
*/
CNLStore.prototype = {
__proto__: Store.prototype,
_logName: "CNL",
get _wordStm() {
this._log.debug("Creating SQL statement: _wordStm");
let stm = CNL_DAO.dbConn.createStatement(
'SELECT remind, originalUrl, read FROM vocabulary WHERE word = :word');
this.__defineGetter__("_wordStm", function() stm);
return stm;
},
_findWordByGUID: function(guid) {
try {
this._wordStm.params.word = decodeURIComponent(guid);
if (!this._wordStm.step())
return null;
return {
remind: encodeURIComponent(this._wordStm.row.remind),
originalUrl: this._wordStm.row.originalUrl,
read: this._wordStm.row.read
};
}
finally {
this._wordStm.reset();
}
},
itemExists: function(id) {
this._log.debug("CNLStore.itemExists..." + id);
// Return true if an item with given guid exists in the store.
// guid we use here is the "word" collumn in vocabulary DB.
// see: https://wiki.mozilla.org/Labs/Weave/ClientAPI#Writing_a_Store_class
if (this._findWordByGUID(id))
return true;
return false;
},
createRecord: function(guid, cryptoMetaURL) {
this._log.debug("CNLStore.createRecord..." + guid);
let foo = this._findWordByGUID(guid);
let record = new CNLRec();
record.id = guid;
if (foo) {
record.remind = foo.remind;
record.originalUrl = foo.originalUrl;
record.read = foo.read;
record.encryption = cryptoMetaURL;
}
else
record.deleted = true;
this.cache.put(guid, record);
return record;
},
changeItemID: function (oldID, newID) {
// Find the item with guid = oldId and change its guid to newId.
//do nothing! see http://adblockplus.org/blog/adding-weave-support-to-an-extension
},
getAllIDs: function() {
// Return a list of the GUIDs of all items. Invent GUIDs for any items
// that don't have them already, and remember the mapping for later use.
let stm = CNL_DAO.dbConn.createStatement('SELECT word FROM vocabulary');
let items = {};
try {
while (stm.step()) {
items[encodeURIComponent(stm.row.word)] = true;
}
}
finally {
stm.reset();
}
return items;
},
wipe: function() {
// Delete everything!
//now: do nothing
},
create: function(record) {
this._log.debug("CNLStore.create...");
// Create a new item based on the values in record
CNL_DAO.create(record.getDecodeData());
},
update: function(record) {
this._log.debug("CNLStore.update...");
// look up the stored record with id = record.id, then set
// its values to those of new record
CNL_DAO.update(record.getDecodeData());
},
remove: function(record) {
this._log.debug("CNLStore.remove...");
// look up the stored record with id = record.id, then delete it.
CNL_DAO.remove(decodeURIComponent(record.id));
}
};
Utils.deferGetSet(CNLRec, "cleartext", ["word", "originalUrl", "remind", "read"]);
///////////////////////////////////////////////////////
function CNLTracker() {
this._init();
}
CNLTracker.prototype = {
__proto__: Tracker.prototype,
name: "cnl",
_logName: "CNLTracker",
file: "cnl",
_init: function HT__init() {
Tracker.prototype._init.call(this);
/* Here is where you would register your tracker as an observer, so that
its onEvent() (or other appropriately named) method can be called
in response to events. */
//see https://wiki.mozilla.org/Labs/JS_Modules#Observers
Observers.add("cnl_wordAddOrModify", this.onWordAddOrModify, this);
},
onWordAddOrModify: function (word, unuseParam) {//function(subject, data)
if (this.ignoreAll)
return;
this._log.trace("onWordAddOrModify: " + word);
if (this.addChangedID(encodeURIComponent(word)))
this._score ++;
}
};
//////////
function CNLService(){
Utils.delay(this._registerEngine, 7000, this, "_startupTimer");
}
CNLService.prototype = {
_registerEngine: function(){
//Weave.Engines.register(Weave["CNLEngine"]);
Engines.register(CNLEngine);
}
}
var cnlService = new CNLService();